package com.bobgao.my_first_demo.controller;

import com.bobgao.my_first_demo.entity.UserEntity;
import com.bobgao.my_first_demo.req.UserReq;
import com.bobgao.my_first_demo.resp.CommonResp;
import com.bobgao.my_first_demo.resp.PageResp;
import com.bobgao.my_first_demo.req.UserSaveReq;
import com.bobgao.my_first_demo.service.UserService;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("/getList")
    public CommonResp getList(UserReq userReq){
//        CommonResp commonResp = new CommonResp();
//        commonResp.setSuccess();
//        commonResp.setMessage();
//        commonResp.setContent();
//        return commonResp;

        CommonResp<PageResp<UserEntity>> resp = new CommonResp<>();
        PageResp<UserEntity> list = userService.getList(userReq);
        resp.setContent(list);
        return resp;
    }

    @DeleteMapping("/delete")
    public CommonResp delete(String id){
        CommonResp<UserEntity> resp = new CommonResp<>();
        userService.delete(Long.parseLong(id));
        return resp;
    }

    @PostMapping("/save")
    public CommonResp save(@Valid @RequestBody UserSaveReq req){
        req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes()));
        CommonResp<UserEntity> resp = new CommonResp<>();
        userService.save(req);
        return resp;
    }
}
